package com.weng.cloud.service8881.concurrenct.algorithm.recursion.component;

import java.util.concurrent.CountDownLatch;

/**
 * @Author: weng
 * @Date: 2022/7/9
 * @Description: com.weng.cloud.service8881.concurrenct.algorithm.recursion.component
 */

public class ValueLatch<T> {
    private T value = null;
    private final CountDownLatch done = new CountDownLatch(1);

    public boolean isSet() {
        return done.getCount() == 0;
    }

    public synchronized void setValue(T newValue) {
        if (isSet()) {
            return;
        }
        value = newValue;
        done.countDown();
    }

    public T getValue() throws InterruptedException {
        done.await();
        synchronized (this) {
            return value;
        }
    }
}
